package com.flute.icrawler.htmlparser.dom;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.cyberneko.html.parsers.DOMParser;
import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.flute.icrawler.htmlparser.HtmlParserException;

public class HtmlParser {

	public HtmlParser(String url) {
		this.url = null;
		this.url = url;
	}

	public HtmlParser() {
		url = null;
	}

	public Document parser(InputStream in, int type) throws HtmlParserException {
		if (in == null)
			throw new HtmlParserException("init params is null");
		if (type == 2)
			return cobraParser(in);
		else
			return nekoParser(in);
	}

	private Document cobraParser(InputStream in) throws HtmlParserException {
		Logger.getLogger("org.lobobrowser").setLevel(Level.OFF);
		Document document;
		try {
			SimpleUserAgentContext context = new SimpleUserAgentContext();
			context.setExternalCSSEnabled(false);
			context.setScriptingEnabled(true);
			DocumentBuilderImpl dbi = new DocumentBuilderImpl(context);
			document = dbi.parse(new InputSourceImpl(in, url, "utf-8"));
		} catch (MalformedURLException e) {
			throw new HtmlParserException((new StringBuilder(
					"connect url error")).append(e.getMessage()).toString());
		} catch (SAXException e) {
			throw new HtmlParserException((new StringBuilder(
					"parser html document error")).append(e.getMessage())
					.toString());
		} catch (IOException e) {
			throw new HtmlParserException((new StringBuilder(
					"Read html source IO error")).append(e.getMessage())
					.toString());
		}
		try {
			in.close();
		} catch (Exception exception1) {
		}
		return document;

	}

	private Document nekoParser(InputStream in) throws HtmlParserException {
		Document document;
		try {
			DOMParser parser = new DOMParser();
			parser.setFeature("http://xml.org/sax/features/namespaces", false);
			parser.setProperty(
					"http://cyberneko.org/html/properties/names/elems", "upper");
			parser.setProperty(
					"http://cyberneko.org/html/properties/names/attrs", "lower");
			parser.parse(new InputSource(new InputStreamReader(in, "utf-8")));
			document = parser.getDocument();
		} catch (SAXException e) {
			logger.log(Level.WARNING, "parser html error");
			throw new HtmlParserException((new StringBuilder(
					"parser html error-SAXException:")).append(e.getMessage())
					.toString());
		} catch (IOException e) {
			logger.log(Level.WARNING, "read html source error");
			throw new HtmlParserException((new StringBuilder(
					"read html source error:")).append(e.getMessage())
					.toString());
		}
		try {
			in.close();
		} catch (IOException ioexception) {
		}
		return document;

	}

	private static Logger logger = Logger.getLogger(HtmlParser.class.getName());
	private String url;

}
